package com.css.fxfzypg.modules.resultManagement.reportManagement.service.Impl;

import com.css.fxfzypg.manage.sampling.repository.SamplingReqository;
import com.css.fxfzypg.manage.sampling.repository.entity.SamplingEntity;
import com.css.fxfzypg.modules.baseData.entity.CountiesCondition;
import com.css.fxfzypg.modules.baseData.entity.HosPeoEntity;
import com.css.fxfzypg.modules.baseData.entity.VillageTownCondition;
import com.css.fxfzypg.modules.baseData.entity.VillageTownHosPeoEntity;
import com.css.fxfzypg.modules.baseData.repository.CountiesConditionRepository;
import com.css.fxfzypg.modules.baseData.repository.HosPeoRepository;
import com.css.fxfzypg.modules.baseData.repository.VillageTownConditionRepository;
import com.css.fxfzypg.modules.baseData.repository.VillageTownHosPeoRepository;
import com.css.fxfzypg.modules.resultManagement.reportManagement.entity.*;
import com.css.fxfzypg.modules.resultManagement.reportManagement.repository.SamplingSurveyReportRepository;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.HackLoopTableRenderPolicy;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Dbj
 * @date 2022年12月08日  14:02
 */
@Service
public class SamplingSurveyReportServiceImpl  {

    @Resource
    private SamplingSurveyReportRepository samplingSurveyReportRepository;

    @Resource
    private SamplingReqository samplingReqository;

    @Resource
    private HosPeoRepository hosPeoRepository;

    @Resource
    private VillageTownHosPeoRepository villageTownHosPeoRepository;

    @Resource
    private CountiesConditionRepository countiesConditionRepository;

    @Resource
    private VillageTownConditionRepository villageTownConditionRepository;
    //@Override
    public ReportManagement createReport(String taskCode,String taskName, HttpServletResponse response) {
        List<SamplingSurveyReport> reportList = new ArrayList<>();
        Map<String, Object> paramMap = new HashMap<>();
        String provinceName = "";
        //查询该任务code有无子任务，如果有子任务则获取所有子任务下的所有的调研点信息，如果不存在子任务则获取该任务下关联的调研点
        List<SamplingEntity> samplingEntityList = samplingReqository.getSamplingEntityList(Arrays.asList(taskCode));
        //构建1-1 调研点信息表
        List<Map<String,Object>> samplingEntityMapList = new ArrayList<Map<String,Object>>();
        //构建调查点概况区县信息
        List<SamplingArea> areaList = new ArrayList<>();
        List<SamplingArea2> areaList2 = new ArrayList<>();
        if(PlatformObjectUtils.isNotEmpty(samplingEntityList)){
            Map<String,List<String>> streetMap = new HashMap<>();
            Map<String,List<String>> villageMap = new HashMap<>();
            for (SamplingEntity samplingEntity : samplingEntityList) {
                if(streetMap.containsKey(samplingEntity.getProvince()+"_"+ samplingEntity.getCity()+"_"+samplingEntity.getArea())){
                    List<String> streetList = streetMap.get(samplingEntity.getProvince()+"_"+ samplingEntity.getCity()+"_"+samplingEntity.getArea());
                    streetList.add(samplingEntity.getTownship());
                }else{
                    List<String> steetList = new ArrayList<>();
                    steetList.add(samplingEntity.getTownship());
                    streetMap.put(samplingEntity.getProvince()+"_"+ samplingEntity.getCity()+"_"+samplingEntity.getArea(),steetList);
                }
                if(villageMap.containsKey(samplingEntity.getProvince()+"_"+ samplingEntity.getCity()+"_"+samplingEntity.getArea()+"_"+samplingEntity.getTownship())){
                    List<String> surveyList = streetMap.get(samplingEntity.getProvince()+"_"+ samplingEntity.getCity()+"_"+samplingEntity.getArea()+"_"+samplingEntity.getTownship());
                    surveyList.add(samplingEntity.getFieldSurveyName());
                }else{
                    List<String> surveyList = new ArrayList<>();
                    surveyList.add(samplingEntity.getFieldSurveyName());
                    villageMap.put(samplingEntity.getProvince()+"_"+ samplingEntity.getCity()+"_"+samplingEntity.getArea()+"_"+samplingEntity.getTownship(),surveyList);
                }
            }
            provinceName = samplingEntityList.get(0).getProvince();
            int y =1;
            int z =2;
            int db = 1;
            Map<String,SamplingEntity> pointMap = new HashMap<>();
            for (int i=0;i<samplingEntityList.size();i++) {

                SamplingArea samplingArea = new SamplingArea();
                //查询区县人口和建筑物概况信息
                HosPeoEntity hosPeo = hosPeoRepository.findHosPeo(samplingEntityList.get(i).getProvince(), samplingEntityList.get(i).getCity(), samplingEntityList.get(i).getArea());
                //查询农村人口和建筑物概况信息
                VillageTownHosPeoEntity villageHosPeo = villageTownHosPeoRepository.findHosPeo(samplingEntityList.get(i).getProvince(), samplingEntityList.get(i).getCity(), samplingEntityList.get(i).getArea(),samplingEntityList.get(i).getTownship());
                //查询区县概况信息
                CountiesCondition countiesCondition = countiesConditionRepository.getQxCountiesCodition(samplingEntityList.get(i).getProvince(), samplingEntityList.get(i).getCity(), samplingEntityList.get(i).getArea());
                //查询乡镇概况信息
                VillageTownCondition villageTownCondition = villageTownConditionRepository.getVillageCountiesCodition(samplingEntityList.get(i).getProvince(), samplingEntityList.get(i).getCity(), samplingEntityList.get(i).getArea(),samplingEntityList.get(i).getTownship());
                SamplingArea area = new SamplingArea();
                SamplingArea2 area2 = new SamplingArea2();

                Map<String, Double> doubleStr = buildSortMap(samplingEntityList.get(i));
                Map<String, Double> yearStr = buildSortYearMap(samplingEntityList.get(i));
                StringBuilder mainStructureTypeBuilder = new StringBuilder();
                StringBuilder conditionBuilder = new StringBuilder();
                if(doubleStr.values().size()>0){
                    doubleStr.forEach((k,v)->{
                        mainStructureTypeBuilder.append(k).append("、");
                        conditionBuilder.append(k).append("占比约").append(v).append("%").append(",");
                    });
                }
                conditionBuilder.append("房屋建造年代情况：");
                if(yearStr.values().size()>0){
                    yearStr.forEach((k,v)->{
                        conditionBuilder.append(k).append("占比约").append(v).append("%").append(",");
                    });
                }
                area2.setHouseFeature(z+"."+(i+1)+db+"."+"房屋特点");
                area2.setAreaNameTitle(z+"."+(i+1)+samplingEntityList.get(i).getArea());
                area2.setAreaNameBody(samplingEntityList.get(i).getArea());
                area2.setTownNameTitle(z+"."+(i+1)+db+"."+(i+1)+"."+samplingEntityList.get(i).getTownship());
                area2.setTownNameBody(samplingEntityList.get(i).getTownship());
                area2.setSurveyName(samplingEntityList.get(i).getFieldSurveyName());
                db++;
                //area2.setEvaluation(z+"."+(i+1)+db+"."+"抗震能力评价");
                String mainStructureType = mainStructureTypeBuilder.toString();
                mainStructureType = mainStructureType.substring(0,mainStructureType.length()-1);
                String structureCondition = conditionBuilder.toString();
                structureCondition = structureCondition.substring(0,structureCondition.length()-1);
//                area2.setStructureStyle(mainStructureType);
//                area2.setStructureCondition(structureCondition);
                if(hosPeo!=null){
                    //获取调查点概况城镇结构类型占比
                    List<Map<String, Object>> townStructureList = returnAreaTownStructureList(hosPeo);
                    //获取调查点概况农村结构类型占比
                    List<Map<String, Object>> villageStructureList = returnAreaVillageStructureList(hosPeo);
                    area.setAreaNameTitle(y+"."+(i+1)+samplingEntityList.get(i).getArea());
                    area.setAreaNameBody(samplingEntityList.get(i).getArea());
                    area.setArea(String.valueOf(hosPeo.getDhpCountyArea().intValue()));
                    area.setPopulation(String.valueOf(hosPeo.getDhpTotalPop().intValue()));
                    area.setHouseRatio(String.valueOf(hosPeo.getDhpHouseTatio().intValue())+"%");
                    area.setAreaTownStructureList(townStructureList);
                    area.setAreaVillageStructureList(villageStructureList);

               }
                if(countiesCondition!=null){
                    area.setCcNation(countiesCondition.getCcNation());
                    area.setCcEconomy(countiesCondition.getCcEconomy());
                    area.setCcLandform(countiesCondition.getCcLandform());
                    area.setCcClimate(countiesCondition.getCcClimate());
                    area.setCcSeismotectonics(countiesCondition.getCcSeismotectonics());
                    area.setCcTraffic(countiesCondition.getCcTraffic());
                    area.setCcDisaster(countiesCondition.getCcDisaster());
                    area.setCcFacility(countiesCondition.getCcFacility());
                    area.setCcHistory(countiesCondition.getCcHistory());
                    area.setCcTrait(countiesCondition.getCcTrait());
                    area.setCcHazard(countiesCondition.getCcHazard());
                    area.setCcReservoir(countiesCondition.getCcReservoir());
                }
                List<SamplingTown> townist = new ArrayList<>();

                List<String> steetList = streetMap.get(samplingEntityList.get(i).getProvince()+"_"+samplingEntityList.get(i).getCity()+"_"+samplingEntityList.get(i).getArea());
                for (int j=0;j<steetList.size();j++) {
                    SamplingTown town = new SamplingTown();
                    if(villageHosPeo.getProvince().equals(samplingEntityList.get(i).getProvince())
                            && villageHosPeo.getCity().equals(samplingEntityList.get(i).getCity()) &&
                            villageHosPeo.getPrefecture().equals(samplingEntityList.get(i).getArea()) &&
                            villageHosPeo.getVillageTown().equals(steetList.get(j))){
                        List<Map<String, Object>> townStructureList = returnTownStructureList(villageHosPeo);
                        List<Map<String, Object>> villageStructureList = returnVillageStructureList(villageHosPeo);
                        town.setTownNameTitle(y+"."+(i+1)+"."+(j+1)+steetList.get(j)).setTownNameBody(steetList.get(j))
                                .setArea(villageHosPeo.getDhpCountyArea().toString()).setPopulation(villageHosPeo.getDhpTotalPop().toString()).setHouseRatio(String.valueOf(villageHosPeo.getDhpHouseTatio().intValue())+"%")
                                .setTownStructureList(townStructureList)
                                .setVillageStructureList(villageStructureList);
                    }
                    if(PlatformObjectUtils.isNotEmpty(villageTownCondition)){
                        if(villageTownCondition.getProvince().equals(samplingEntityList.get(i).getProvince())
                                && villageTownCondition.getCity().equals(samplingEntityList.get(i).getCity()) &&
                                villageTownCondition.getPrefecture().equals(samplingEntityList.get(i).getArea()) &&
                                villageTownCondition.getVillageTown().equals(steetList.get(j))){
                            town.setCcNation(villageTownCondition.getCcNation());
                            town.setCcEconomy(villageTownCondition.getCcEconomy());
                            town.setCcLandform(villageTownCondition.getCcLandform());
                            town.setCcClimate(villageTownCondition.getCcClimate());
                            town.setCcSeismotectonics(villageTownCondition.getCcSeismotectonics());
                            town.setCcTraffic(villageTownCondition.getCcTraffic());
                            town.setCcDisaster(villageTownCondition.getCcDisaster());
                            town.setCcFacility(villageTownCondition.getCcFacility());
                            town.setCcTrait(villageTownCondition.getCcTrait());
                            town.setCcHazard(villageTownCondition.getCcHazard());
                            town.setCcReservoir(villageTownCondition.getCcReservoir());
                        }
                    }
                    List<String> villageList = villageMap.get(samplingEntityList.get(i).getProvince()+"_"+samplingEntityList.get(i).getCity()+"_"+samplingEntityList.get(i).getArea()+"_"+samplingEntityList.get(i).getTownship());
                    List<SamplingVillage> samplingVillageList = new ArrayList<>();
                    if(PlatformObjectUtils.isNotEmpty(villageList)){
                        for (int i1 = 0; i1 < villageList.size(); i1++) {
                            SamplingVillage samplingVillage = new SamplingVillage();
                            samplingVillage.setVillageNameTitle(y+"."+(i+1)+"."+(j+1)+"."+(i1+1)+villageList.get(i1)).setVillageNamebody(villageList.get(i1))
                             .setTownName(steetList.get(j)).setHouseNum(samplingEntityList.get(i).getTotalHouseholdNum())
                             .setPopulation(samplingEntityList.get(i).getTotalPopulation()).setResident(samplingEntityList.get(i).getActualResidentPopulation())
                             .setOuter(samplingEntityList.get(i).getMigrantWorkers()).setChild(samplingEntityList.get(i).getChildrenProportion())
                             .setAuditNum(samplingEntityList.get(i).getYoungProportion()).setOldNum(samplingEntityList.get(i).getOldAgeProportion())
                             .setLoadStatus(samplingEntityList.get(i).getTrafficConditions()).setLocation(samplingEntityList.get(i).getEnvironment())
                             .setSchoolStatus("是".equals(samplingEntityList.get(i).getIsSchool())?"有学校，学生数量约"+samplingEntityList.get(i).getStudentNum()+"人，教室"+samplingEntityList.get(i).getClassroomNum()+"间":"无学校")
                             .setHospital("是".equals(samplingEntityList.get(i).getIsHospital())?"有医院，医生"+samplingEntityList.get(i).getDoctorNum()+"名，床位数"+samplingEntityList.get(i).getBedNum()+"个":"无医院")
                             .setDisasterPoint("是".equals(samplingEntityList.get(i).getIsDisaster())?"有"+samplingEntityList.get(i).getDisasterType()+""+samplingEntityList.get(i).getDisasterScale():"周边无地质灾害")
                             .setReser("是".equals(samplingEntityList.get(i).getIsReservoir())?"有水库":"无水库");
                            samplingVillageList.add(samplingVillage);
                        }
                    }
                    town.setVillageList(samplingVillageList);
                    townist.add(town);
                }
                area.setTownList(townist);
                areaList.add(area);
                areaList2.add(area2);

                Map<String,Object> map = new HashMap<String, Object>();
                //省
                map.put("province", samplingEntityList.get(i).getProvince());
                //县
                map.put("area", samplingEntityList.get(i).getArea());
                //乡镇
                map.put("township",samplingEntityList.get(i).getTownship());
                //调研点名称
                map.put("surveyName",samplingEntityList.get(i).getFieldSurveyName());

                samplingEntityMapList.add(map);
            }
        }


        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        SamplingSurveyReport report = SamplingSurveyReport.builder()
//                .taskName(taskName)
//                .createTime(sdf.format(date))
//                .samplingInfoList(samplingEntityMapList)
                .areaList(new ArrayList<>())
                .areaList2(new ArrayList<>())
               .build();
        List<SamplingArea> townNameList = report.getAreaList();
        List<SamplingArea2> townNameList2 = report.getAreaList2();
        townNameList.addAll(areaList);
        townNameList2.addAll(areaList2);
        reportList.add(report);
        paramMap.put("taskName",taskName);
        paramMap.put("provinceName",provinceName);
        paramMap.put("createTime",sdf.format(date));
        paramMap.put("samplingInfoList",samplingEntityMapList);
        paramMap.put("section1",reportList);
        // 生成文件
        Map<String, String> resultMap = createReport(paramMap);
        return null;
    }


    private Map<String, Double> buildSortYearMap(SamplingEntity samplingEntity) {
        Map<String,Double> map = new HashMap<>(9);
        if(Double.valueOf(samplingEntity.getBuild70sBefore())>0.0){
            map.put("70年代前",Double.valueOf(samplingEntity.getHighStructure()));
        }
        if(Double.valueOf(samplingEntity.getBuild70s())>0.0){
            map.put("70年代",Double.valueOf(samplingEntity.getSteelStructure()));
        }
        if(Double.valueOf(samplingEntity.getBuild80s())>0.0){
            map.put("80年代",Double.valueOf(samplingEntity.getConcreteStructure()));
        }
        if(Double.valueOf(samplingEntity.getBuild90s())>0.0){
            map.put("90年代",Double.valueOf(samplingEntity.getMasonryStructure()));
        }
        if(Double.valueOf(samplingEntity.getBuild00s())>0.0){
            map.put("00年代",Double.valueOf(samplingEntity.getBrickWoodStructure()));
        }
        if(Double.valueOf(samplingEntity.getBuild10s())>0.0){
            map.put("10年代",Double.valueOf(samplingEntity.getSoilStructure()));
        }
        if(Double.valueOf(samplingEntity.getBuild20s())>0.0){
            map.put("20年代",Double.valueOf(samplingEntity.getWoodStructure()));
        }
        List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {

            @Override
            public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
                return (int) (o2.getValue() - o1.getValue());
            }
        });
        Map result = new LinkedHashMap();
        for (Map.Entry<String, Double> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

    private Map<String,Double> buildSortMap(SamplingEntity samplingEntity) {
        Map<String,Double> map = new HashMap<>(9);
        if(Double.valueOf(samplingEntity.getHighStructure())>0.0){
            map.put("高层结构",Double.valueOf(samplingEntity.getHighStructure()));
        }
        if(Double.valueOf(samplingEntity.getSteelStructure())>0.0){
            map.put("钢结构",Double.valueOf(samplingEntity.getSteelStructure()));
        }
        if(Double.valueOf(samplingEntity.getConcreteStructure())>0.0){
            map.put("多层钢筋混凝土结构",Double.valueOf(samplingEntity.getConcreteStructure()));
        }
        if(Double.valueOf(samplingEntity.getMasonryStructure())>0.0){
            map.put("砖混结构",Double.valueOf(samplingEntity.getMasonryStructure()));
        }
        if(Double.valueOf(samplingEntity.getBrickWoodStructure())>0.0){
            map.put("砖木结构",Double.valueOf(samplingEntity.getBrickWoodStructure()));
        }
        if(Double.valueOf(samplingEntity.getSoilStructure())>0.0){
            map.put("土结构",Double.valueOf(samplingEntity.getSoilStructure()));
        }
        if(Double.valueOf(samplingEntity.getWoodStructure())>0.0){
            map.put("木结构",Double.valueOf(samplingEntity.getWoodStructure()));
        }
        if(Double.valueOf(samplingEntity.getStoneStructure())>0.0){
            map.put("石层结构",Double.valueOf(samplingEntity.getStoneStructure()));
        }
        if(Double.valueOf(samplingEntity.getOtherStructure())>0.0){
            map.put("其他结构",Double.valueOf(samplingEntity.getOtherStructure()));
        }
        List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {

            @Override
            public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
                return (int) (o2.getValue() - o1.getValue());
            }
        });
        Map result = new LinkedHashMap();
        for (Map.Entry<String, Double> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

    private List<Map<String, Object>> returnVillageStructureList(VillageTownHosPeoEntity hosPeo) {
        List<Map<String,Object>> villageStructureList = new ArrayList<>(9);
        Map<String,Object> townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "高层建筑");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageHighTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "钢结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageSteelTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "多层钢筋混凝土结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageBetonTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "砖混结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageBrickTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "砖木结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageMasonryTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "土结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageSoilTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "木结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageWoodTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "石结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageStoneTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "其他结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageOtherTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        return villageStructureList;
    }

    private List<Map<String, Object>> returnTownStructureList(VillageTownHosPeoEntity hosPeo) {
        List<Map<String,Object>> townStructureList = new ArrayList<>(9);
        Map<String,Object> townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "高层建筑");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownHighTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "钢结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownSteelTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "多层钢筋混凝土结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownBetonTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "砖混结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownBrickTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "砖木结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownMasonryTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "土结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownSoilTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "木结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownWoodTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "石结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownStoneTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "其他结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownOtherTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        return townStructureList;
    }

    private List<Map<String, Object>> returnAreaVillageStructureList(HosPeoEntity hosPeo) {
        List<Map<String,Object>> villageStructureList = new ArrayList<>(9);
        Map<String,Object> townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "高层建筑");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageHighTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "钢结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageSteelTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "多层钢筋混凝土结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageBetonTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "砖混结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageBrickTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "砖木结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageMasonryTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "土结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageSoilTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "木结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageWoodTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "石结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageStoneTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "其他结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpVillageOtherTatio().intValue())+"%");
        villageStructureList.add(townStructureMap);
        return villageStructureList;
    }

    private List<Map<String,Object>> returnAreaTownStructureList(HosPeoEntity hosPeo){
        List<Map<String,Object>> townStructureList = new ArrayList<>(9);
        Map<String,Object> townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "高层建筑");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownHighTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "钢结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownSteelTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "多层钢筋混凝土结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownBetonTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "砖混结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownBrickTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "砖木结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownMasonryTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "土结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownSoilTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "木结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownWoodTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "石结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownStoneTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        townStructureMap = new HashMap<>();
        townStructureMap.put("structureTypeName", "其他结构");
        townStructureMap.put("ratio", String.valueOf(hosPeo.getDhpTownOtherTatio().intValue())+"%");
        townStructureList.add(townStructureMap);
        return townStructureList;
    }
    private static Map<String, String> createReport(Map<String, Object> paramMap) {
        // 文件存放根目录
        String rootPath = "D:" + File.separator+"report"+ File.separator;
        File dir = new File(rootPath);
        // 文件夹不存在则创建
        if (!dir.exists()) {
            boolean mkdir = dir.mkdir();
        }
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String nowStr = sdf.format(now);
        String docxPath =  rootPath + "预评估现场调查报告" +nowStr+ ".docx";
        String imgPath = rootPath + "image" + File.separator;
        // 获取电子协查函模板
        String path = "D:\\1.docx";
        // 根据模板渲染word、并且生成pdf文件
        ClassPathResource classPathResource = new ClassPathResource("static/预评估现场调查报告模板.docx");
        try (InputStream inputStream = classPathResource.getInputStream()) {
            //渲染表格  动态行
            HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
            Configure config = Configure.newBuilder()
                    .bind("samplingInfoList", policy)
                    .bind("areaTownStructureList",policy)
                    .bind("areaVillageStructureList",policy)
                    .bind("townStructureList",policy)
                    .bind("villageStructureList",policy).build();
            // 通过协查函模板，开始生成电子协查函
            try (
                XWPFTemplate template = XWPFTemplate.compile(inputStream,config).render(paramMap);
                OutputStream outDocx = Files.newOutputStream(Paths.get(docxPath))) {
                NiceXWPFDocument xwpfDocument = template.getXWPFDocument();
                // 输出word
                template.getXWPFDocument().write(outDocx);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, String> map = new HashMap<>(4);
        map.put("rootPath", rootPath);
        map.put("docxPath", docxPath);
        return map;
    }
}
